package com.escape.subject.domain.handler.subject;

import com.escape.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-26  11:58
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class SubjectTypeHandlerFactory {

    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlerList;

    private Map<SubjectInfoTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();

    public SubjectTypeHandler getHandler(int subjectType){
        SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
        return handlerMap.get(subjectInfoTypeEnum);
    }

    /**
     * @PostConstruct 注解被标注的方法会在属性依赖注入之后被调用
     * 同样的类实现 InitializingBean 的 afterPropertiesSet 方法也有同样的效果
     */
    @PostConstruct
    public void init () {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlerList) {
            handlerMap.put(subjectTypeHandler.getSubjectType(), subjectTypeHandler);
        }
    }
}
